package com.zd.ticketing.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zd.common.bean.model.HardwareInfo;
import com.zd.common.bean.model.ScenicSpot;
import com.zd.common.bean.model.ScenicSpotExt;
import com.zd.common.common.utils.StringUtils;
import com.zd.common.dao.HardwareInfoDao;
import com.zd.common.dao.ScenicSpotDao;
import com.zd.common.dao.ScenicSpotExtDao;
import com.zd.common.dao.common.BaseDao;
import com.zd.common.dao.common.FindRequestEntity;
import com.zd.common.dao.common.WhereEnum;
import com.zd.common.service.impl.BaseServiceImpl;
import com.zd.ticketing.bean.vo.ScenicSpotDetailVo;
import com.zd.ticketing.service.IScenicSpotService;

/**
 * @author lishi
 * @date 2018年11月8日 上午9:26:15
 */
@Service
public class ScenicSpotServiceImpl extends BaseServiceImpl<ScenicSpot> implements IScenicSpotService {
	@Autowired
	private ScenicSpotDao scenicSpotDao;
	
	@Autowired
	private ScenicSpotExtDao scenicSpotExtDao;
	
	@Autowired
	private HardwareInfoDao hardwareInfoDao;

	@Override
	public BaseDao<ScenicSpot> getBaseDao() {
		return scenicSpotDao;
	}

	@Override
	public HardwareInfo getHardwareInfo(HardwareInfo hardwareInfo) throws Exception {
		FindRequestEntity findReq=new FindRequestEntity();
		if(!StringUtils.isBank(hardwareInfo.getAppId()))
		findReq.addWhere("app_id", WhereEnum.EQ, hardwareInfo.getAppId());
		findReq.addWhere("device_no", WhereEnum.EQ, hardwareInfo.getDeviceNo());
		if(!StringUtils.isBank(hardwareInfo.getQrDeviceNo()))
		findReq.addWhere("qr_device_no", WhereEnum.EQ, hardwareInfo.getQrDeviceNo());
		if(!StringUtils.isBank(hardwareInfo.getIdDeviceNo()))
		findReq.addWhere("id_device_no", WhereEnum.EQ, hardwareInfo.getIdDeviceNo());
		List<HardwareInfo> hardwareInfoList = hardwareInfoDao.find(findReq);
		if(hardwareInfoList != null && hardwareInfoList.size() > 0){
			return hardwareInfoList.get(0);
		}
		return null;
	}

	@Override
	public ScenicSpotDetailVo getScenicSpotDetail(Integer scenicSpotId) throws Exception {
		ScenicSpot scenicSpot = get(scenicSpotId);
		if(scenicSpot != null) {
			ScenicSpotDetailVo resultVo = new ScenicSpotDetailVo();
			BeanUtils.copyProperties(scenicSpot, resultVo);
			FindRequestEntity findReq = new FindRequestEntity();
			// 获取扩展信息
			findReq = new FindRequestEntity();
			findReq.addWhere("scenic_spot_id", WhereEnum.EQ, scenicSpot.getId());
			List<ScenicSpotExt> extList = scenicSpotExtDao.find(findReq);
			if(extList != null && extList.size() > 0) {
				ScenicSpotExt ext = extList.get(0);
				BeanUtils.copyProperties(ext, resultVo);
				resultVo.setId(scenicSpot.getId());
			}
			return resultVo;
		}
		return null;
	}
	
	@Override
	public ScenicSpotDetailVo getScenicSpotDetail(String appid) throws Exception {
		FindRequestEntity findReq=new FindRequestEntity();
		findReq.addWhere("app_id", WhereEnum.EQ, appid);
		List<ScenicSpot> scenicSpotList = scenicSpotDao.find(findReq);
		if(scenicSpotList != null && scenicSpotList.size() >0){
			ScenicSpot scenicSpot = scenicSpotList.get(0);
			if(scenicSpot != null) {
				ScenicSpotDetailVo resultVo = new ScenicSpotDetailVo();
				BeanUtils.copyProperties(scenicSpot, resultVo);
				
				// 获取扩展信息
				findReq = new FindRequestEntity();
				findReq.addWhere("scenic_spot_id", WhereEnum.EQ, scenicSpot.getId());
				List<ScenicSpotExt> extList = scenicSpotExtDao.find(findReq);
				if(extList != null && extList.size() > 0) {
					ScenicSpotExt ext = extList.get(0);
					BeanUtils.copyProperties(ext, resultVo);
					resultVo.setId(scenicSpot.getId());
				}
				return resultVo;
			}
		}
		return null;
	}
}
